package com.foreknow.Test;

import com.foreknow.entity.User;
import com.foreknow.db.C3P0Utils;
import com.foreknow.entity.User;
import com.foreknow.util.DbHelper;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayHandler;

import java.sql.SQLException;

public class Demo {
    public static void main(String[] args) {
        try {
            queryDemo01();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // ArrayHandler处理类的使用
    //将结果集中的额第一条记录封装到一个object[]数组中
    public static void queryDemo01() throws SQLException {
        // 1.创建QueryRunner对象
        QueryRunner qr = new QueryRunner(DbHelper.getDataSource());
        // 2.执行查询
        Object[] objs = qr.query("select * from user where id = ?", new ArrayHandler(), 1);
        for(Object o: objs){		// object[]中保存了object对象
            System.out.println(o);
        }
    }

    public boolean addMessage(User user) throws SQLException {
        QueryRunner qr = new QueryRunner(DbHelper.getDataSource());
        String sql = "insert into user(username,phone,email,id,password) values(?,?,?,?,?,?)";
        int x = qr.update(sql,user.getId());
        return x==1;
    }
    }



